/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import Model.M_Customer;
import Model.M_Account;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author thuydao
 */
@WebServlet(urlPatterns = {"/C_Customer"})
public class C_Customer extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
        
            String action = request.getParameter("btAction");
            if(action.equals("Search"))
            {
                 //M_Customer cus = new M_Customer();
            //M_Customer[] result = cus.show_KH();
                String search=request.getParameter("txtSearch");
            
                M_Customer cus = new M_Customer();
                M_Customer[] result = cus.search_KH(search);
             
//                
                request.setAttribute("INFO_search", result);
                
                RequestDispatcher rd = request.getRequestDispatcher("Search_Customer.jsp");
                rd.forward(request, response);
                
               
            }
            else if(action.equals("Delete"))
            {
                String makh = request.getParameter("makh");
                String search = request.getParameter("txtSearch");
                
                M_Customer cus = new M_Customer();
                cus.setMakh(makh);
                boolean result_TK = cus.delete_TK();
                boolean result_KH = cus.delete_KH();
                
               //response.sendRedirect("C_Show_Customer");
                String url = "C_Customer?btAction=Search&txtSearch=" + search;
                RequestDispatcher rd = request.getRequestDispatcher("C_Show_Customer");
                rd.forward(request, response);
                
            }
            else if(action.equals("Update"))
            {
                
               String makh = request.getParameter("U_txtmakh");
               String tenkh = request.getParameter("U_txttenkh");
               String gioitinh = request.getParameter("U_txtgioitinh");
               String sdt = request.getParameter("U_txtsdt");
               String diachi = request.getParameter("U_txtdiachi");
               String cmnd = request.getParameter("U_txtcmnd");
               
               M_Customer cus = new M_Customer(makh, tenkh, gioitinh, sdt, diachi, cmnd);
               boolean result = cus.update_KH();
               RequestDispatcher rd = request.getRequestDispatcher("C_Show_Customer");
               rd.forward(request, response);
               //response.sendRedirect("C_Show_Customer");
            }
            else if(action.equals("Update account"))
            {
                
               String makh = request.getParameter("Acc_txtmakh");
               String tenkh = request.getParameter("Acc_txttenkh");
               String gioitinh = request.getParameter("Acc_txtgioitinh");
               String sdt = request.getParameter("Acc_txtsdt");
               String diachi = request.getParameter("Acc_txtdiachi");
               String cmnd = request.getParameter("Acc_txtcmnd");
               
               M_Customer cus = new M_Customer(makh, tenkh, gioitinh, sdt, diachi, cmnd);
               boolean result = cus.update_KH();
               RequestDispatcher rd = request.getRequestDispatcher("Welcome.jsp");
               rd.forward(request, response);
               //response.sendRedirect("C_Show_Customer");
            }
            else if(action.equals("Update password"))
            {
               String tentk = "${sessionScope.USER}";
               String matkhau = request.getParameter("Acc_txtmatkhau");
               String makh = request.getParameter("Acc_txtmakh");
               M_Account acc = new M_Account();
               boolean result = acc.Update_pass(tentk, matkhau);
               
               if(result)
               {
                   RequestDispatcher rd = request.getRequestDispatcher("Welcome.jsp");
                   rd.forward(request, response);
               }
               
               //response.sendRedirect("C_Show_Customer");
            }
            else if(action.equals("Account"))
            {
                    String tendn = request.getParameter("tendn");
                    //out.println(tendn);
                    M_Customer cus = new M_Customer();
                    M_Customer[] result = cus.search_KH_TenDN(tendn);
                
                    request.setAttribute("INFOAccount", result);
                    //response.sendRedirect("Customer.jsp");
                    RequestDispatcher rd = request.getRequestDispatcher("Info_Account.jsp");
                    rd.forward(request, response);
            }
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
